package com.kotlin.book.adapter

import android.content.Intent
import android.graphics.Typeface
import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.kotlin.book.R
import com.kotlin.book.activity.DetailActivity
import com.kotlin.book.databinding.ItemHomeUpdateBinding
import com.kotlin.book.entry.Book
import com.kotlin.book.util.Util

class HomeUpdateItemAdapter(private var itemList: List<Book>) :
    RecyclerView.Adapter<HomeUpdateItemAdapter.ItemViewHolder>() {
    class ItemViewHolder(itemBinding: ItemHomeUpdateBinding) :
        RecyclerView.ViewHolder(itemBinding.root) {
        val bookName: TextView = itemBinding.bookName
        val bookProfile: TextView = itemBinding.bookProfile
        val bookImage: ImageView = itemBinding.bookImage
        var tagBox = itemBinding.homeTagBox
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): HomeUpdateItemAdapter.ItemViewHolder {
        val binding =
            ItemHomeUpdateBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ItemViewHolder(binding)
    }

    override fun onBindViewHolder(holder: HomeUpdateItemAdapter.ItemViewHolder, position: Int) {
        val item = itemList[position]
        holder.bookName.text = item.name
        holder.bookProfile.text = item.blurb
        Util.getImage(item.pic, holder.bookImage, holder.itemView.context)

        val oneTags = item.tag?.split(",") ?: emptyList()
        for (tag in oneTags) {
            if (tag == "") {
                continue
            }
            val textView = TextView(holder.itemView.context).apply {
                layoutParams = LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT
                ).apply {

                    setMargins(Util.dpToPx(12, context), 0, 0, 0) // 设置 marginStart
                }
                background = ContextCompat.getDrawable(context, R.drawable.home_tag_background)
                setPadding(
                    Util.dpToPx(10, context),
                    Util.dpToPx(4, context),
                    Util.dpToPx(10, context),
                    Util.dpToPx(4, context)
                ) // 设置 padding
                setTextColor(ContextCompat.getColor(context, R.color.primary_color)) // 设置文本颜色
                textSize = 12f // 设置文本大小（sp）
                setTypeface(null, Typeface.BOLD) // 设置加粗
                text = tag // 设置文本
            }
            holder.tagBox.addView(textView)
        }

        holder.itemView.setOnClickListener {
            // 页面跳转
            val intent = Intent(holder.itemView.context, DetailActivity::class.java)
            val bundle = Bundle()
            // 传参
            bundle.putInt("id", item.id)
            intent.putExtras(bundle)
            holder.itemView.context.startActivity(intent)
        }
    }

    override fun getItemCount(): Int {
        return itemList.size
    }

    fun setData(newList:List<Book>) {
        itemList = newList
        notifyDataSetChanged()
    }
}